﻿<?php
class Upload extends CI_Controller{
    var $data = array();
	function __construct(){
		parent::__construct();
		$this->load->helper(array('url','download'));
		$this->load->model('M_upload');
		$this->load->library(array('form_validation','pagination'));
	}
	function index(){
		if ($this->M_upload->is_logged_in()){
			redirect('upload/login'); 
		}
		$this->form_validation->set_rules('username','用户名','required',array('required'=>'用户名不能为空!'));
		$this->form_validation->set_rules('password','密码','required',array('required'=>'密码不能为空!'));
		$this->data['login_error'] = "";
		if($this->form_validation->run()) {
			$username = $this->input->post('username');
			$password = $this->input->post('password');
			if ($user = $this->M_upload->get_by_username($username)) {	
				if($this->M_upload->check_password($password,$user['password'])){
					$this->M_upload->allow_pass($username);
					redirect('upload/login');
				}else{
					$this->data['login_error'] = "登录失败:无效的用户名或密码!";
				}
			}else{
				$this->data['login_error'] = "登录失败:无效的用户名或密码!";
			}
		}
		$this->load->view('ulogin/header',$this->data);
		$this->load->view('ulogin/serbody');
		$this->load->view('ulogin/footer');
	}
	function dosearch(){
		$downloadcode = $this->input->post('downloadcode');
		$res = $this->M_upload->get_file_bycode($downloadcode);
		if($res){
			$this->data['login_error'] = "";
			$this->data['search_error'] = "";
			$this->data['res'] = $res;
			$this->data['number'] = 1;
			$this->load->view('ulogin/header',$this->data);
			$this->load->view('ulogin/resbody');
			$this->load->view('ulogin/footer');
		}else{
			$this->data['login_error'] = "";
			$this->data['search_error'] = "未找到文件!";
			$this->load->view('ulogin/header',$this->data);
			$this->load->view('ulogin/resbody');
			$this->load->view('ulogin/footer');
		}
	}
	function getypefile(){
		$filetype = $this->input->post('filetype');
		$res = $this->M_upload->get_file_bytype($filetype);
		if($res){
			$this->data['login_error'] = "";
			$this->data['search_error'] = "";
			$this->data['res'] = $res;
			$this->data['number'] = 1;
			$this->load->view('ulogin/header',$this->data);
			$this->load->view('ulogin/resbody');
			$this->load->view('ulogin/footer');
		}else{
			$this->data['login_error'] = "";
			$this->data['search_error'] = "未找到文件!";
			$this->load->view('ulogin/header',$this->data);
			$this->load->view('ulogin/resbody');
			$this->load->view('ulogin/footer');
		}
	}
	function login($error=''){
		if ($this->M_upload->is_logged_in()){
			$this->data['username'] = $this->session->userdata('user');
			$result = $this->M_upload->get_by_username($this->session->userdata('user'));
			$this->data['number'] = 1;
			$this->data['userright'] = $result['userright'];
			$this->data['userspace'] = $result['userspace'];
			$this->data['spacerate'] = $result['userspace']/1024*20;
			if($error==1){
				$this->data['error'] = "未选中上传文件!";
			}else{
				$this->data['error'] = "";
			}
			$perPage = 5;
			$config['base_url'] = site_url('upload/login');
			$config['total_rows'] = $this->M_upload->get_file_num($this->data['username']);
			$config['per_page'] = $perPage;
			$config['uri_segment'] = 3;
			$config['first_link'] = '首页';
			$config['prev_link'] = '上一页';
			$config['next_link'] = '下一页';
			$config['last_link'] = '尾页';
			$config['first_tag_open'] = '<span aria-hidden="true">';
			$config['first_tag_close'] = '</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
			$config['prev_tag_open'] = '<span>';
			$config['prev_tag_close'] = '</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
			$config['num_tag_open'] = '<span>';
			$config['num_tag_close'] = '</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
			$config['cur_tag_open'] = '<b>';
			$config['cur_tag_close'] = '</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
			$config['next_tag_open'] = '<span>';
			$config['next_tag_close'] = '</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
			$config['last_tag_open'] = '<span>';
			$config['last_tag_close'] = '</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
			$config['use_page_numbers'] = TRUE;
			$this->pagination->initialize($config);
			$this->data['links'] = $this->pagination->create_links();
			
			$pagenum = $this->uri->segment(3);
			if($pagenum == 0){
				$pagenum = 1;
			}
			$offset = ($pagenum-1)*$perPage;
			$res = $this->M_upload->get_user_file($this->data['username'],$perPage,$offset);
			$this->data['pages'] = ceil($this->M_upload->get_file_num($this->data['username'])/$perPage);
			$this->data['perpage'] = $perPage;
			$this->data['items'] = $this->M_upload->get_file_num($this->data['username']);
			$this->data['res'] = $res;	
			
			
			$this->load->view('hlogin/header',$this->data);
			$this->load->view('hlogin/resbody',$this->data);
			$this->load->view('hlogin/footer');
			
		}else{
			$this->data['login_error'] = "尚未登录,请先登录!";
			$this->load->view('ulogin/header',$this->data);
			$this->load->view('ulogin/serbody');
			$this->load->view('ulogin/footer');
		}
		
	}
	function do_upload(){
		if ($this->M_upload->is_logged_in()){
			$config['upload_path']  = './uploads/';
			$config['allowed_types'] = 'txt|gif|jpg|png';
			$config['max_size'] = 100;
			$config['max_width'] = 1024;
			$config['max_height'] = 768;
			$this->load->library('upload', $config);
			$this->data['error'] = '';
			if (!$this->upload->do_upload('userfile')){
				$this->data['error'] = $this->upload->display_errors();
				redirect('upload/login/1');
			}else{
				$info = $this->upload->data();
				$fileinfo = array();
				$fileinfo['filename'] = $info['file_name'];
				$fileinfo['filepath'] = $info['full_path'];
				$fileinfo['filetype'] = $this->input->post('filetype');
				$fileinfo['filesize'] = $info['file_size'];
				$fileinfo['upauthor'] = $this->session->userdata('user');
				$fileinfo['checknum'] = rand(10000,99999);
				$fileinfo['ispublic'] = $this->input->post('fileright');
				$fileinfo['uptime'] = time();
				if($this->M_upload->insert_file_info($fileinfo)){
					$res = $this->M_upload->calcu_space(0,$fileinfo['filesize'],$this->session->userdata('user'));
					if($res == 1){
						$data['info'] = "上传成功!";
					}else if($res == 0){
						$data['info'] = "剩余空间不足!";
					}else{
						$data['info'] = "上传失败!";
					}
					$this->load->view('upload_success',$data);
				}
			}
		}else{
			$this->data['login_error'] = "尚未登录,请先登录!";
			$this->load->view('ulogin/header',$this->data);
			$this->load->view('ulogin/serbody');
			$this->load->view('ulogin/footer');
		}
    }
	function dropitems($filename,$username){
		$res1 = $this->M_upload->drop_user_file($filename,$username);
		$res2 = $this->M_upload->calcu_space(1,$res1['filesize'],$username);
		if($res1 && $res2){
			if(unlink($res1['filepath'])){
				$data['info'] = "删除成功!";
			}else{
				$data['info'] = "释放失败!";
			}
			
		}else{
			$data['info'] = "删除失败!";
		}
		$this->load->view('upload_success',$data);
	}
	
	function logout(){
		$this->M_upload->remove_pass();
		$this->session->sess_destroy();
		redirect('upload/index');
	}
}
?>